package serialer

import (
	"bytes"
	"sync"
)

//SafeBuffer Tread safe
type SafeBuffer struct {
	b  bytes.Buffer
	rw sync.RWMutex
}

//Read read
func (b *SafeBuffer) Read(p []byte) (n int, err error) {
	b.rw.RLock()
	defer b.rw.RUnlock()
	return b.b.Read(p)
}

//Write write
func (b *SafeBuffer) Write(p []byte) (n int, err error) {
	b.rw.Lock()
	defer b.rw.Unlock()
	return b.b.Write(p)
}

//Reset reset
func (b *SafeBuffer) Reset() {
	b.rw.RLock()
	defer b.rw.RUnlock()
	b.b.Reset()
}

//Len len
func (b *SafeBuffer) Len() int {
	b.rw.RLock()
	defer b.rw.RUnlock()
	return b.b.Len()
}
